/**
 * Created by Alice on 2016/9/19 0019
 */
var status = 0;
var page_size = 15;
$(function () {
    // 时间插件
    //查询的开始时间默认给今天
    $('#startTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $('#endTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        // console.log(ev.date.valueOf());
    });


    $("#_search").click(function () {
        init();
    });
    $("#sys_court_status").find("a").attr("href", r.base_path + "system_monitor/sys_court_status?courtUid=" + r.court_id);
    $("#sys_play_info").find("a").attr("href", r.base_path + "system_monitor/sys_play_info?court=Uid" + r.court_uid);
    $("#sys_fault_condition").find("a").attr("href", r.base_path + "views/system_monitor/sys_fault_condition.html?court=" + r.court_id);
    $("#sys_meeting").find("a").attr("href", r.base_path + "views/system_monitor/sys_meeting.html?court=" + r.court_id);
    $("#sys_server_status").find("a").attr("href", r.base_path + "system_monitor/sys_server_status?courtUid=" + r.court_uid);
    $("#sys_equipment_management").find("a").attr("href", r.base_path + "views/system_monitor/sys_equipment_management.html?court=" + r.court_id);
    $("#sys_log_info").find("a").attr("href", r.base_path + "system_monitor/sys_log_info?courtUid=" + r.court_uid);
    //初始化法院列表
    select_all_child_courts();
});

function init() {
    if (r.flag_search) {
        status = 1;
        // countAllWarnInfo();
    } else {
        status = 0;
        // countWarnInfo();
    }
    selectAllWarnInfo();
}
function select_all_child_courts() {
    var select_child_courts = $("#selectAllChildCourts");
    select_child_courts.empty();
    select_child_courts.append("<option value=''>" + '所有法院' + "</option>");
    $.each(r.court_list, function (i, n) {
        select_child_courts.append("<option value='" + n.uid + "'>" + n.name + "</option>");
    });
    select_child_courts.val(r.court_id == r.user_court_id ? '' : r.court_id);
    init();
};


//条件查询报警信息总数量
// function countWarnInfo() {
//     var search_form = $('#search_form');
//     var information = search_form.find("select[name='operationInformation']").val();
//     var courtId = search_form.find("#selectAllChildCourts").val();
//     // var operationTime = search_form.find("input[name='operationTime']").val();
//     // var completeTime = search_form.find("input[name='completeTime']").val();
//     var startTime = $("#startTime").find("input").val();
//     var endTime = $("#endTime").find("input").val();
//
//     if (information < 0) {
//         information = null;
//     }
//     $.get(r.base_path + "sys/countWarnInfo", {
//         courtId: courtId,
//         information: information,
//         startTime: startTime,
//         endTime: endTime
//     }, function (result) {
//         if (result && result.status == 0) {
//             if (result.data == 0) {
//                 page(1, 1, null);
//                 $('#loading').hide();
//             } else {
//                 page(1, Math.ceil(result.data / 15), selectWarnInfo);
//             }
//             selectWarnInfo(1);
//         } else {
//             $('#loading').hide();
//         }
//     }, 'json')
// }

// function selectWarnInfo(pageNo) {
//     var search_form = $('#search_form');
//     var information = search_form.find("select[name='operationInformation']").val();
//     var courtId = search_form.find("#selectAllChildCourts").val();
//     // var operationTime = search_form.find("input[name='operationTime']").val();
//     // var completeTime = search_form.find("input[name='completeTime']").val();
//     var startTime = $("#startTime").find("input").val();
//     var endTime = $("#endTime").find("input").val();
//     if (information < 0) {
//         information = null;
//     }
//     $.get(r.base_path + "sys/selectWarnInfo", {
//         courtId: courtId,
//         information: information,
//         startTime: startTime,
//         endTime: endTime,
//         pageNo: pageNo,
//         pageSize: 15
//     }, function (result) {
//         $('#tableDivId').empty();
//         if (result.data) {
// //             console.log(result);
//             var tr = ' <table id="table" class="table table-bordered text-center" ></table>';
//             $('#tableDivId').append(tr);
//             $('#table').bootstrapTable({
//                 rowStyle: function rowStyle(row, index) {
//                     if (row.type == "系统警告") {
//                         return {classes: 'text-nowrap another-class', css: {"color": "yellow"}}
//                     }
//                     return {}
//                 },
//                 columns: [{
//                     field: 'courtId',
//                     title: '法院名称'
//                 }, {
//                     field: 'equipment',
//                     title: '报警设备'
//                 }, {
//                     field: 'time',
//                     title: '报警时间'
//                 }, {
//                     field: 'type',
//                     title: '报警类型'
//                 }, {
//                     field: 'detailinfo',
//                     title: '详细信息'
//                 }
//                 ],
//                 data: result.data
//             });
//             $("#loading").hide();
//             $(".table").show();
//         }
//     }, 'json')
// }
//
// function countAllWarnInfo() {
//     var search_form = $('#search_form');
//     var information = search_form.find("select[name='operationInformation']").val();
//     var courtId = search_form.find("#selectAllChildCourts").val();
//     // var operationTime = search_form.find("input[name='operationTime']").val();
//     // var completeTime = search_form.find("input[name='completeTime']").val();
//     var startTime = $("#startTime").find("input").val();
//     var endTime = $("#endTime").find("input").val();
//
//     if (information < 0) {
//         information = null;
//     }
//     $.get(r.base_path + "sys/countAllWarnInfo", {
//         courtId: courtId,
//         information: information,
//         startTime: startTime,
//         endTime: endTime
//     }, function (result) {
//         if (result) {
//             if (result.data == 0) {
//                 page(1, 1, null);
//                 $('#loading').hide();
//             } else {
//                 page(1, Math.ceil(result.data / 15), selectAllWarnInfo);
//             }
//             selectAllWarnInfo(1);
//         }
//     }, 'json')
// }

function selectAllWarnInfo(page_num) {
    if (!page_num) {
        page_num = 1
    }
    //搜索框中的法院uid
    if ($("#selectAllChildCourts").val()) {
        courtUid = $("#selectAllChildCourts").val();
    }
    var search_form = $('#search_form');
    var information = search_form.find("select[name='operationInformation']").val();
    var courtId = search_form.find("#selectAllChildCourts").val();
    // var operationTime = search_form.find("input[name='operationTime']").val();
    // var completeTime = search_form.find("input[name='completeTime']").val();
    var startTime = $("#startTime").find("input").val();
    var endTime = $("#endTime").find("input").val();

    if (information < 0) {
        information = null;
    }
    $.get(r.base_path + "warn/selectWarnInfoList", {
        courtUid: courtUid,
        status: status,
        warningType: information,
        startDate: startTime,
        endDate: endTime,
        page: page_num,
        pageSize: page_size
    }, function (result) {
        $('#tableDivId').empty();
        var countTotal = result.data.count;
        if (countTotal <= 0) {
            page(1, 1, null);
        } else {
            page(page_num, Math.ceil(countTotal / page_size), selectAllWarnInfo);
        }
        if (result.data.waringMapList) {
            var tr = ' <table id="table" class="table table-bordered text-center" ></table>';
            $('#tableDivId').append(tr);
            $('#table').bootstrapTable({
                rowStyle: function rowStyle(row, index) {
                    if (row.type == "系统警告") {
                        return {classes: 'text-nowrap another-class', css: {"color": "yellow"}}
                    }
                    return {}
                },
                columns: [{
                    field: 'courtName',
                    title: '法院名称'
                }, {
                    field: 'deviceName',
                    title: '报警设备'
                }, {
                    field: 'time',
                    title: '报警时间'
                }, {
                    field: 'type',
                    title: '报警类型'
                }, {
                    field: 'detail',
                    title: '详细信息'
                }
                ],
                data: result.data.waringMapList
            });
            $("#loading").hide();
            $("#table").show();
        }
    }, 'json')
}

function page(now, totalPages, callback) {
    base.pagination('#_page', {
        currentPage: now,
        totalPages: totalPages,
        onPageClicked: function (page) {
            callback(page);
        }
    });
}
